/**
 * The Community Project
 * Copyright Team1 Productions
 * 
 * Joshua Amick
 * Stephen Cunningham
 * Thema Davis
 * Tomika Dawson
 * Lucie DeCristofaro
 * 
 * All Rights Reserved.
 */

package com.actions;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.constants.UserPermissions;
import com.domain.User;
import com.forms.CreateAnnOrEventForm;

public class SaveAnnouncementOrEventRequestAction extends CommunityBaseAction{
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		super.execute(mapping, form, request, response);

		// Cast the actionForm, if there is one.
		ActionForward forward;
		CreateAnnOrEventForm f = (CreateAnnOrEventForm) form;
		Timestamp startDate = null;
		Timestamp endDate = null;		
		User user = ((User)request.getSession().getAttribute("AUTH_USER"));

		String strErrors = "";
		
		//Get object type
		if (f.getAnnOrEvent()==null||f.getAnnOrEvent().length()==0)
			strErrors += "You must specify whether this is an announcement or an event</br>";
		else
		{
			if (f.getAnnOrEvent().equals("Event"))
			{
				// location is required for events
				if (f.getLocation()==null||f.getLocation().length()==0)
					strErrors += "You must specify a location for an event</br>";
			}				
		}
		
		//Date formatting
		startDate = f.getDateTimestamp(f.getStartYear(), f.getStartMonth(), f.getStartDay(), f.getStartHour(), f.getStartMinutes(), f.getStartAmOrPm());
		if (startDate==null||this.checkDate(f.getStartYear(), f.getStartMonth(), f.getStartDay()))
			strErrors += "Invalid Start Date or Time, check days and months.</br>";
		
		endDate = f.getDateTimestamp(f.getEndYear(), f.getEndMonth(), f.getEndDay(), f.getEndHour(), f.getEndMinutes(), f.getEndAmOrPm());
		if (endDate==null||this.checkDate(f.getEndYear(), f.getEndMonth(), f.getEndDay()))
			strErrors += "Invalid End Date or Time, check days and months.</br>";
		
		if(startDate!=null&&endDate!=null&&endDate.compareTo(startDate)!=1)
		{
			System.out.println("s="+startDate.toString()+" e="+endDate.toString());
			strErrors += "The start date must be earlier than the end date<br>";			
		}
		
		//Checking for title
		if (f.getTitle()==null||f.getTitle().length()==0)
			strErrors += "You must specify the title</br>";
			
		//if (f.getAvailability()==null||f.getAvailability().length()==0)
		//	strErrors += "You must specify the availability</br>";
		
//		boolean publicView = true;
//		if(f.getAvailability()!=null&&f.getAvailability().equals("false")){
//			publicView = false;
//		}
		
		if (strErrors.length()>0)
		{
			// needs more checking than this
			forward = mapping.findForward("failure");
			request.setAttribute("ERRORS", strErrors);		
			// send the form back so that no information gets lost
			request.setAttribute("FORM",f);
		}
		else if (f.getAnnOrEvent().equals("Announcement"))
		{
			// save the announcement
			boolean approved = false;
			if(user.getUserPermissions()==UserPermissions.ADMIN){
				approved = true;
			}
			
			this.biz.getPrivateMgr().saveAnnouncement(null,user, startDate, endDate, f.getTitle(), f.getDescription(), 
					f.getDetails(), true, approved);
			forward = mapping.findForward("success");
			request.setAttribute("INFO", "The announcement has been submitted.");
			request.setAttribute("FORM",f);
		}
		else
		{			
			
			boolean approved = false;
			if(user.getUserPermissions()==UserPermissions.ADMIN){
				approved = true;
			}
			// save the event
			this.biz.getPrivateMgr().saveEvent(null, user, startDate, endDate, false, f.getTitle(), 
					f.getDescription(), f.getLocation(), f.getDetails(), true, approved);
			forward = mapping.findForward("success");
			request.setAttribute("INFO", "The event has been submitted.");
			request.setAttribute("FORM",f);
		}
		return forward;
	}
}